<template>
  <div class="recommend">
  
  <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >

    <div class="recommendValueBox">
      
      <Recommend v-for="item in recommendData" :key="item.id" :data="item">
        <span class="yuanjia">¥{{ item.market_price }}</span>
        <span class="remai">热卖中</span>
        <span class="kuchun">剩余{{ item.stock_quantity }}</span>
      </Recommend>

    </div>
  
      </van-list>
    </van-pull-refresh>
   

  </div>
</template>

<script>
import { recommend } from "@/api/homeApi.js";
import Recommend from "@/components/recommend.vue";
export default {
  name: "shuma",
  data() {
    return {
      recommendData: [],
      pagesize: 10,
      pageindex:1,

      loading: false,
      finished: false,
      refreshing: false,
    };
  },
  methods: {
    async recommendAxios(size,index) {
      let { data } = await recommend(size, index);
      // ...data.message能拿到message数组里的所有对象,如果是data.message拿到的就是message这个数组
      this.recommendData.push(...data.message);

      // 判断是否还有数据,如果没有就把finished设为true,也就是让页面的底部显示"没有更多了"
      if(data.message.length < 10){
          this.finished = true
      }
    },
    // onLoad上拉加载时触发的函数
    onLoad() {
      setTimeout(() => {
        
        // 上拉加载
        if(this.loading == true){
          this.pageindex++
          this.recommendAxios(this.pagesize,this.pageindex)
        }


          // 下拉刷新
        if (this.refreshing) {
          this.recommendData = [];
          this.refreshing = false;
        }

        this.loading = false;

        }, 600);
    },
    // onRefresh下拉刷新时触发的函数
    onRefresh() {
     
      // 清空列表数据
      this.finished = false;

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },
  },
  created() {
    this.recommendAxios(this.pagesize,this.pageindex);
  },
  components: {
    Recommend,
  },
  
};
</script>

<style lang="scss" scoped>
$ui-width: 720;
$num: 15;
$baseFontSize: 50; // 750 / 15
@function px2rem($px) {
  @return $px / $baseFontSize + rem;
}

.recommend {
  padding: 15px 10px;
  background-color: #f0f0f0;
  position: relative;

  .recommendFont {
    width: 100px;
    background-color: #f0f0f0;
    text-align: center;
    z-index: 1;
    position: relative;
    display: block;
    font-size: px2rem(16);
    margin: auto;
  }

  hr {
    position: absolute;
    top: 0;
    left: 5%;
    width: 90%;
  }

  .recommendValueBox {
    margin-top: 20px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    span {
      font-size: px2rem(14);
    }
    .remai {
      width: 50%;
    }
  }
}
</style>